package com.workcenter.app;

import java.util.Date;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.TextView;

import com.ppcodes.core.data.DXml;
import com.ppcodes.core.utils.UtiDialogsFactory;
import com.workcenter.base.UIBase;
import com.workcenter.entity.NetClient;
import com.workcenter.entity.PubConst;
import com.workcenter.entity.Session;
import com.workcenter.entity.UrlParams;
import com.workcenter.entity.model.ECustomerInfo;
import com.workcenter.entity.model.ERepairsInfo;

public class UINewRepairs extends UIBase
{
   private EditText edtCustomerArea;
   private EditText edtCustomerName;
   private EditText edtRoomName;
   private EditText edtInstrument;
   private EditText edtInstrumentID;
   private EditText edtRepairsStaff;
   private EditText edtRepairsPhone;
   private TextView txtPriority;
   private TextView txtIsStop;
   private EditText edtDiagnosis;

   private TextView txtRepairDate;
   private TableRow trRepairDate;
   private TableRow trPriority;
   private TableRow trIsStop;

   private Button btnSearch;
   private Button btnOK;
   private Button btnCancel;

   private TextView txtTitle;

   private void InitControls()
   {
	  txtTitle = (TextView) findViewById(R.id.txtTitle_toptoolbar_no_btn);
	  txtTitle.setText(getString(R.string.title_newrepairs));

	  Date time = new Date();
	  String monthS = String.valueOf(time.getMonth() + 1);
	  String dayS = String.valueOf(time.getDate());
	  txtRepairDate = (TextView) findViewById(R.id.txtRepairDate_newrepair);
	  txtRepairDate.setText(String.format("%s-%s-%s", String.valueOf(time.getYear() + 1900), monthS.length() == 1 ? ("0" + monthS) : monthS, dayS.length() == 1 ? ("0" + dayS) : dayS));

	  trRepairDate = (TableRow) findViewById(R.id.trRepairDate_newrepair);
	  trRepairDate.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			final DatePicker dp = new DatePicker(UINewRepairs.this);
			final Time today = new Time();
			today.setToNow();
			UtiDialogsFactory.getInstance().ShowCustomViewDialogWithOKCancel(UINewRepairs.this, null, dp, new DialogInterface.OnClickListener()
			{
			   @Override
			   public void onClick(DialogInterface dialog, int which)
			   {
				  if ((dp.getYear() > today.year) || (dp.getYear() == today.year && dp.getMonth() > today.month)
						|| (dp.getYear() == today.year && dp.getMonth() == today.month && dp.getDayOfMonth() >= today.monthDay))
				  {
					 String month = String.valueOf(dp.getMonth() + 1);
					 String day = String.valueOf(dp.getDayOfMonth());
					 txtRepairDate.setText(String.format("%s-%s-%s", String.valueOf(dp.getYear()), month.length() == 1 ? ("0" + month) : month, day.length() == 1 ? ("0" + day) : day));
					 dialog.dismiss();
				  }
				  else
				  {
					 UtiDialogsFactory.getInstance().ShowOKAlertDialog(UINewRepairs.this, getString(R.string.dialog_tips), getString(R.string.newrepair_date_early_than_today));
				  }
			   }
			});
		 }
	  });

	  edtCustomerArea = (EditText) findViewById(R.id.edtCustomerArea_newrepair);
	  edtCustomerName = (EditText) findViewById(R.id.edtCustomerName_newrepair);
	  edtDiagnosis = (EditText) findViewById(R.id.edtDiagnosis_newrepair);
	  edtInstrument = (EditText) findViewById(R.id.edtInstrument_newrepair);
	  edtInstrumentID = (EditText) findViewById(R.id.edtInstrumentId_newrepair);
	  final String[] strIsStop = { "已停机", "未停机" };
	  txtIsStop = (TextView) findViewById(R.id.txtIsStop_newrepair);
	  trIsStop = (TableRow) findViewById(R.id.trIsStop_newrepair);
	  trIsStop.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			UtiDialogsFactory.getInstance().ShowItemsDialog(UINewRepairs.this, strIsStop, new DialogInterface.OnClickListener()
			{
			   @Override
			   public void onClick(DialogInterface dialog, int which)
			   {
				  txtIsStop.setText(strIsStop[which]);
			   }
			});
		 }
	  });
	  final String[] strPriority = { "紧急", "特急", "一般" };
	  txtPriority = (TextView) findViewById(R.id.txtPriority_newrepair);
	  trPriority = (TableRow) findViewById(R.id.trPriority_newrepair);
	  trPriority.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			UtiDialogsFactory.getInstance().ShowItemsDialog(UINewRepairs.this, strPriority, new DialogInterface.OnClickListener()
			{
			   @Override
			   public void onClick(DialogInterface dialog, int which)
			   {
				  txtPriority.setText(strPriority[which]);
			   }
			});
		 }
	  });
	  edtRepairsPhone = (EditText) findViewById(R.id.edtRepairPhone_newrepair);
	  edtRepairsStaff = (EditText) findViewById(R.id.edtRepairStaff_newrepair);
	  edtRoomName = (EditText) findViewById(R.id.edtRoomName_newrepair);

	  btnSearch=(Button)findViewById(R.id.btnSearch_newrepair);
	  btnSearch.setOnClickListener(new View.OnClickListener()
	  {
	     @Override
	     public void onClick(View v)
	     {
	  	    Intent intent=new Intent();
	  	    intent.setClass(UINewRepairs.this, UICustomerList.class);
	  	    startActivityForResult(intent, PubConst.ActivityCode.UICustomerList);
	     }
	  });
	  btnOK = (Button) findViewById(R.id.btnOK_newrepair);
	  btnOK.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			executeAsyncTask(PubConst.TaskName.AddNewRepair);
		 }
	  });
	  btnCancel = (Button) findViewById(R.id.btnCancel_newrepair);
	  btnCancel.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			setResult(RESULT_CANCELED);
			finish();
		 }
	  });
   }

   private boolean VerifyInfo()
   {
	  if (edtCustomerName.getText().toString().trim() == null || edtCustomerName.getText().toString().trim().equals(""))
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UINewRepairs.this, getString(R.string.newrepair_customername_noempty));
		 return false;
	  }
	  else if (edtDiagnosis.getText().toString().trim() == null || edtDiagnosis.getText().toString().trim().equals(""))
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UINewRepairs.this, getString(R.string.newrepair_diagnosis_noempty));
		 return false;
	  }
	  else if (edtInstrument.getText().toString().trim() == null || edtInstrument.getText().toString().trim().equals(""))
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UINewRepairs.this, getString(R.string.newrepair_instrument_noempty));
		 return false;
	  }
	  return true;
   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(UINewRepairs.this, getString(R.string.dialog_uploading));
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  try
	  {
		 if (taskName != null && taskName.equals(PubConst.TaskName.AddNewRepair))
		 {
			ERepairsInfo info = new ERepairsInfo();
			info.setCustomerArea(edtCustomerArea.getText().toString().trim());
			info.setCustomerName(edtCustomerName.getText().toString().trim());
			info.setDiagnosis(edtDiagnosis.getText().toString().trim());
			info.setInstrument(edtInstrument.getText().toString().trim());
			info.setInstrumentID(edtInstrumentID.getText().toString().trim());
			info.setIsStop(txtIsStop.getText().toString().trim().equals(getString(R.string.all_plase_select)) ? "" : txtIsStop.getText().toString().trim());
			info.setPriority(txtPriority.getText().toString().trim().equals(getString(R.string.all_plase_select)) ? "" : txtPriority.getText().toString().trim());
			info.setRepairsPhone(edtRepairsPhone.getText().toString().trim());
			info.setRepairsStaff(edtRepairsStaff.getText().toString().trim());
			info.setRoomName(edtRoomName.getText().toString().trim());
			info.setRepairsDate(txtRepairDate.getText().toString());
			return NetClient.InsertNewRepairs(info);
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UINewRepairs_onAsyncTaskInBackground(taskName, params) ", e);
		 }
	  }
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  try
	  {
		 UtiDialogsFactory.getInstance().DismissLoadingDialog();
		 if (result != null)
		 {
			DXml data = (DXml) result;
			if (taskName.equals(PubConst.TaskName.AddNewRepair))
			{
			   if (data.getHead().GetFieldValue("RspCode").equals(UrlParams.SuccessCode))
			   {
				  UtiDialogsFactory.getInstance().ShowOKAlertDialog(UINewRepairs.this, getString(R.string.dialog_tips), getString(R.string.dialog_uploading_success),
						new DialogInterface.OnClickListener()
						{
						   @Override
						   public void onClick(DialogInterface dialog, int which)
						   {
							  setResult(RESULT_OK);
							  finish();
						   }
						});
			   }
			   else
			   {
				  UtiDialogsFactory.getInstance().ShowToastInfoShort(UINewRepairs.this, data.getHead().GetFieldValue("RspMsg"));
			   }
			}
		 }
		 else
		 {
			UtiDialogsFactory.getInstance().ShowToastInfoShort(UINewRepairs.this, getString(R.string.dialog_server_failed));
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UINewRepairs_onAsyncTaskEnd(taskId, taskName, params, result) ", e);
		 }
	  }
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.new_repair);
	  InitControls();
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {
      if(requestCode==PubConst.ActivityCode.UICustomerList)
      {
    	 if(resultCode==RESULT_OK)
    	 {
    		ECustomerInfo customerInfo=(ECustomerInfo)data.getSerializableExtra(PubConst.Intent_Key.CustomerInfo);
    		if(customerInfo!=null)
    		{
    		   edtCustomerName.setText(customerInfo.getCustomerName());
    		   edtInstrument.setText(customerInfo.getInstrument());
    		   edtInstrumentID.setText(customerInfo.getInstrumentID());
    		   edtRoomName.setText(customerInfo.getRoomName());
    		   edtCustomerArea.setText(customerInfo.getArea());
    		}
    	 }
      }
   }
   
   
}
